/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.tests;

import blueprint4j.utils.*;

import java.sql.*;

public class TestLog {
	static int thread_logs_done=0;

	public static void main(String args[]){
		Settings.setSettingsStore(new SettingsStoreMemory());
		Log.info.out("Logging Test Begin");
		long start=System.currentTimeMillis();
		int t=10;
		for (t=0;t<1000;t++){
			Log.trace.out("Trace "+t,"trace message / \\");
			Log.debug.out("Debug "+t,"debug message / \\");
		}
		double ms_perlog=((double)(System.currentTimeMillis()-start))/(t*2);
		// Without
		start=System.currentTimeMillis();
		for (t=0;t<50000;t++){
			Log.trace.out("Trace "+t,"trace message");
		}
		double ms_perunseenlog=((double)(System.currentTimeMillis()-start))/t;
		// Cleanup
		start=System.currentTimeMillis();
		//int num=Log.doCleanUp();
		int num = 0;
		double ms_cleanuplog=(double)(System.currentTimeMillis()-start);
		int repeats=360000;
		int threads=20;
		int cleanups=50;
		// Now run thread test
		for (t=0;t<threads;t++){
			new TestThread(repeats).start();
		}
		start=System.currentTimeMillis();
		while (thread_logs_done<threads*repeats){
			try {
				Thread.sleep(100);
			}catch (Exception whocares){
			}
		}
		double ms_perduallog=((double)(System.currentTimeMillis()-start))/thread_logs_done;
		Log.critical.out("Close test","Per log "+ms_perlog+"ms\rPer offlog "+ms_perunseenlog+"ms\rPer log in "+threads+" threads "+ms_perduallog+"ms");
	}

	static class TestThread extends Thread {
		int logs;

		TestThread(int logs){
			this.logs=logs;
		}

		public void run(){
			for (int t=0;t<logs;t++){
				Log.debug.out("Log test #"+(t+1));
				thread_logs_done++;
			}
		}
	}
}

